// import Vue from "vue";
// import getPageName from "./getPageName";
// import {getPageTree} from "./getPageTree";

// /**
//  * 根据id返回对应的虚拟dom对象
//  * @param {*} id 
//  */
// Vue.prototype.getCurrentVDO = function (id, page = getPageName()) {
//     console.log("getCurrentVDO", id, page);
//     return getPageTree(this, page, id);
// }

import Vue from "vue";
import {getPageTree} from "./getPageTree";
import addORupdate from "../utils/TemplateAxios/add";
import deepCopy from "../utils/deepCopy";
import {MGDialog} from "../ui-components";
import getPageName from "../utils/gw/getPageName";
import {initComponentsList} from "../helper/drag-plugin/initComponents";
let indexs = {
	header: 0,
	main: 1,
	footer: 2,
	0: 0,
	1: 1,
	2: 2,
}
//给页面内的某个组件内的config赋值
Vue.prototype.setEditConfig1 = function (id, config, page = getPageName()) {
    // console.log("这是vue的prototype", this.$store);
    // console.log("这是页面的名称", getPageName());
    // console.log("这是页面的名称", page);
    // getPageTree(this, page);
    let pageTree = this.$store.getters.getDomTree[page];
    // console.log("这是页面的pageTree", pageTree);
    if (id && config) {
    	// console.log(config,"555555555")
        // let exits = false;
        for (let key in pageTree) {
            if (key === "config") {
                continue;
            }
            for (let item of pageTree[key]) {
                if (item.id === id) {
                    //获取到了对应的item，对其config进行合并处理
                    item.config = Object.assign({}, item.config ? item.config : {}, config);
                    // console.log("合并后的config", item.config);
                    this.$store.commit("setDomTree", pageTree);
                    return;
                }
            }

            // console.log("---------继续执行");
        }
    }
}
//给某个页面的config赋值
Vue.prototype.setPageConfig = function (config, page = getPageName()) {
    let pageTree = this.$store.getters.getDomTree[page];
    pageTree.config = Object.assign({}, pageTree.config, config);
    this.$store.commit("setDomTree", pageTree);
}

/**
 * 根据id返回对应的虚拟dom对象
 * @param {*} id 
 */
Vue.prototype.getCurrentVDO1 = function (id, page = getPageName()) {
    // console.log("接收到的id", id)
    // console.log("getPageName", page);
    // if (!page) {
        // page = this.$store.getters["gw/getVirtualPageName"] ? this.$store.getters["gw/getVirtualPageName"] : getPageName();
    // }
    console.log("getCurrentVDO", id, page);
    return getPageTree(this, page, id);
}

//重新给Vuex里的数据赋值
Vue.prototype.resetTemplateParams = function (list) {
    let configParams = this.$store.getters.getConfigParams;
    configParams.templateConfigList = list;
    this.$store.commit("setConfigParams", configParams);
}

//将当前模板的更改保存到数据库中
Vue.prototype.updateDomTreeToDB = function (success, fail, p) {
    //获取Vuex中当前选中的模板的参数，进行参数组装
    let params;
    if (!p) {
        let currentTemplate = this.$store.getters.getCurrentTemplate;
        if (!currentTemplate || !Object.keys(currentTemplate).length) {
            alert("保存失败，没有获取到当前模板的参数。")
        }
        params = Object.assign(currentTemplate, {
            tpConfig: JSON.stringify(window.manifest),
            // tpConfig: testData,
            // enterpriseCode: "NINGJIAXIANSHENG",
            // enterpriseCode: "NJXS",
            // tpState: 0,
            //tpConfig: window.testStr
            // tpConfig: '{"NewThematicActivities":{"header":[{"type":"activitiesbanner","index":0,"id":"NewThematicActivities-activitiesbanner-01","config":{}},{"type":"TabHead","index":1,"id":"NewThematicActivities-TabHead-01","config":{"pages":[{"id":"th43482","name":"特化","createTime":"2018-01-22  21:36","state":"1","comId":"s012335503"}]}}],"content":[{"id":"s012335503","type":"NewZone","index":0,"config":{"state":"1"}}],"footer":[],"config":{"activitiesBgcolor":"#fe0099","activitiesTitle":"分类活动","tabbgcolor":"#fe9900"}},"ThematicActivities":{"header":[{"type":"activitiesbanner","index":0,"id":"ThematicActivities-activitiesbanner-01","config":{}},{"type":"TabHead","index":1,"id":"ThematicActivities-TabHead-01","config":{}}],"content":[],"footer":[],"config":{}},"GoodsForSF":{"header":[],"content":[{"type":"activitiesbanner","id":"xy3589","index":0,"config":{}},{"type":"NewZone","id":"xy9280","index":1,"config":{}},{"type":"activitiesThreePic","id":"xy1277","index":2,"config":{}},{"type":"NewZone","id":"xy2695","index":3,"config":{}},{"type":"activitiesOnePro","id":"xy9541","index":4,"config":{"OneproductStyle":"0"}},{"type":"NewZone","id":"xy9259","index":5,"config":{}},{"type":"activitiesNinePro","id":"xy9853","index":6,"config":{}}],"footer":[],"config":{"activitiesBgcolor":"#9370DB","activitiesTitle":"年货节"}},"NewUser":{"header":[],"content":[{"type":"activitiesbanner","id":"xy8673","index":0,"config":{}},{"type":"NewZone","id":"xy4455","index":6,"config":{}},{"type":"activitiesThreePic","id":"xy7261","index":1,"config":{}},{"type":"NewZone","id":"xy4344","index":2,"config":{}},{"type":"activitiesTwoPro","id":"xy7559","index":3,"config":{"TwoproductStyle":"1"}},{"type":"activitiesTwoPro","id":"xy2585","index":5,"config":{"TwoproductStyle":"0"}},{"type":"activitiesOnePro","id":"xy2821","index":4,"config":{"OneproductStyle":"0"}}],"footer":[],"config":{"activitiesBgcolor":"#F00000","activitiesTitle":"新人专场"}},"index":{"header":[{"type":"test1","index":0,"id":"a0006"}],"content":[{"type":"test2","index":2,"id":"a0002","config":{}},{"type":"test3","index":4,"id":"a0003","config":{"test":"hahahahahah"}},{"type":"test2","index":3,"id":"a0001"},{"type":"test1","index":5,"id":"a0004"},{"type":"banner","index":0,"id":"a00095"},{"type":"HomeCarousel","id":"xy2676","index":1,"config":{}}],"footer":[{"type":"test1","index":0,"id":"a0009"}]},"test":[{"type":"test4","index":0,"id":"a0002"},{"type":"test2","index":1,"id":"a0001"}],"confirmgoodsinfo":[{"type":"list","index":0,"id":"s341554530"},{"type":"header","index":1,"id":"s215213251"}],"address":{"header":[{"type":"addressHeader","index":0,"id":"address002"}],"content":[{"type":"addressList","index":0,"id":"address001"}]},"addAddress":[{"type":"addAddressHeader","index":0,"id":"addAddress002"},{"type":"addAddress","index":1,"id":"addAddress001"},{"type":"addAddressInfo","index":2,"id":"addAddress003"},{"type":"addAddressTag","index":3,"id":"addAddress004"}],"goodsClassify":[{"type":"list","index":0,"id":"con8109"}],"cart":[{"type":"list","index":0,"id":"con8199"}],"CommodityDetail":[{"type":"header","index":0,"id":"s053444524"},{"type":"CommodityDetailinfo","index":1,"id":"s551550032"},{"type":"CommodityDetaildetail","index":2,"id":"s012235124"}],"my":[{"type":"header","index":0,"id":"s233202443"},{"type":"content","index":1,"id":"s003511104"}],"myMessage":[{"type":"myMessage","index":0,"id":"s355554142"}],"searchbar":[{"type":"myheader","index":0,"id":"s444553154"},{"type":"content","index":1,"id":"s250214351"}],"SurroundingStores":[{"type":"myheader","index":0,"id":"s423011153"},{"type":"content","index":1,"id":"s241322400"}],"home":{"header":[{"type":"TabHead","index":0,"id":"home-tabhead-01","config":{"pages":[{"id":"th28250","name":"分类","createTime":"2018-01-11  11:19","state":"1","virtualPage":"selfclassifypage1"},{"id":"th49339","name":"分类2","createTime":"2018-01-11  16:04","state":"1","virtualPage":"selfclassifypage2"},{"id":"th56252","name":"3","createTime":"2018-01-11  16:04","state":"0","virtualPage":"selfclassifypage3"}]}}],"content":[{"type":"HomeNotice","index":3,"id":"con002"},{"type":"NewZone","id":"xy2007","index":7,"config":{}},{"type":"ProductItemNew","id":"xy9897","index":5,"config":{}},{"type":"FeaturedGoodsNew","id":"xy6","index":8,"config":{}},{"type":"HomeCarousel","id":"xy9427","index":6,"config":{}},{"type":"HomeCarousel","id":"xy2606","index":13,"config":{}},{"type":"UserCard","id":"xy4206","index":4,"config":{}},{"type":"HomeCarousel","id":"xy9665","index":0,"config":{}},{"type":"HomeCarousel","id":"xy7663","index":1,"config":{}},{"type":"HomeCarousel","id":"xy1039","index":2,"config":{"bannerAnimation":"0"}},{"type":"FirstClassifyNew","id":"xy585","index":9,"config":{"classfyTitle":true,"classfyImg":true}},{"type":"FirstClassifyNew","id":"xy9128","index":10,"config":{"classfyTitle":true,"classfyImg":true}},{"type":"FirstClassifyNew","id":"xy8592","index":11,"config":{"classfyTitle":true,"classfyImg":true}},{"type":"GuessYouLike","id":"xy5315","index":12,"config":{}}],"footer":[{"type":"HomeFooter","index":0,"id":"con007"}]},"header":[{"type":"activitiesbanner","index":0,"id":"NewThematicActivities-activitiesbanner-01","config":{}},{"type":"TabHead","index":1,"id":"NewThematicActivities-TabHead-01","config":{"pages":[{"id":"th43482","name":"特化","createTime":"2018-01-22  21:36","state":"1","comId":"s012335503"}]}}],"content":[{"id":"s012335503","type":"NewZone","index":0,"config":{"state":"1"}}],"footer":[],"selfclassifypage1":{"header":[],"content":[{"type":"HomeCarousel","id":"xy2999","index":0,"config":{"bannerAnimation":"0"}}],"footer":[{"type":"HomeFooter","index":0,"config":{},"id":"selfclassifypage2"}],"config":{"test":"23323232323"}},"config":{"activitiesBgcolor":"#fe0099","activitiesTitle":"分类活动","tabbgcolor":"#fe9900"},"selfclassifypage2":{"header":[],"content":[],"footer":[{"type":"HomeFooter","index":0,"config":{},"id":"selfclassifypage3"}],"config":{"test":"23323232323"}},"custpage1":{"header":[{"id":"cust-header-01","index":0,"type":"Header","config":{}}],"content":[],"footer":[],"config":{}},"custpage2":{"header":[],"content":[],"footer":[],"config":{}},"custpage3":{"header":[],"content":[],"footer":[],"config":{}},"custpage4":{"header":[],"content":[],"footer":[],"config":{}},"custpage5":{"header":[],"content":[],"footer":[],"config":{}},"custpage6":{"header":[],"content":[],"footer":[],"config":{}},"selfclassifypage3":{"header":[],"content":[],"footer":[{"type":"HomeFooter","index":0,"config":{},"id":"selfclassifypage4"}],"config":{"test":"23323232323"}},"selfclassifypage4":{"header":[],"content":[],"footer":[],"config":{}},"selfclassifypage5":{"header":[],"content":[],"footer":[],"config":{}},"gwTest":{"header":[{"type":"TabHead","index":0,"id":"gwTest-TabHead-01","config":{"pages":[{"id":"th33950","name":"fgfgf","createTime":"2018-01-18  14:27","state":"1","virtualPage":"gwTestselfpage1"},{"id":"th15533","name":"hjhjhk","createTime":"2018-01-18  14:27","state":"1","virtualPage":"gwTestselfpage2"}]}}],"content":[],"footer":[],"config":{}},"gwTestselfpage1":{"header":[],"content":[{"type":"HomeCarousel","id":"xy6555","index":0,"config":{"bannerAnimation":"0"}}],"footer":[{"type":"HomeFooter","index":0,"config":{},"id":"gwTestselfpage1"}],"config":{"test":"23323232323"}},"gwTestselfpage2":{"header":[],"content":[{"type":"UserCard","id":"xy9169","index":0,"config":{}}],"footer":[{"type":"HomeFooter","index":0,"config":{},"id":"gwTestselfpage2"}],"config":{"test":"23323232323"}}}'
            
        });
    } else {
        params = p;
    }
    // delete params.rid;
    console.log('打印params',params);
    addORupdate(params, success, fail);
}

//跳转回首页
Vue.prototype.backHome = function () {
    this.$store.dispatch({
        type: "changeEditItem",
        handleType: "backHome",
        context: this
    });
    return;
    if (!this.itemBeforeLeave()) {
        // console.log("页面发生变化，需要保存");
        //提示用户保存更改
        let flag = window.confirm("要保存当前更改吗？");

        if (flag) {
            let success = (data) => {
                alert("模板保存成功");
                //重新给Vuex里的数据赋值
                this.resetTemplateParams(data);
                //更新副本
                let newCopy = deepCopy(this.getCurrentVDO(this.$store.getters.editConfig.id));
                this.$store.commit("setPageDomItemObjCopy", newCopy);
            }
            let fail = () => {
                alert("模板保存失败");
            }
            this.updateDomTreeToDB(success, fail);
        }
        // return;
    } else {
        // console.log("页面没有发生变化");
        window.location.hash = "/?env=e"
    }
    // console.log("页面没有发生变化");
    // window.location.hash = "/?env=e"
}

Vue.prototype.judgeObjEqual = function (obj1, obj2) {
    //每次进入一个新id对应的编辑区域时，对该id的config进行副本保存
    //拿到这个副本作为初始对象，当切入另一个编辑区域时做对比分析，如果做了更改而没有保存，就提示用户是否保存
    if (!obj1 || !obj2) {
        return true;
    }
    // let currentVOD = getPageTree(this, page, this.$store.getters.editConfig.id);
    let str1 = JSON.stringify(obj1);
    // console.log("str1", str1);
    let str2 = JSON.stringify(obj2);
    // console.log("str2", str2);
    return str1 === str2;

}

//比对，判断是否对当前页面的Obj做了更改
Vue.prototype.itemBeforeLeave = function () {
    let editConfig = this.$store.getters.editConfig;
    //如果editConfig没有值则代表是新进入，不作处理
    if (!(editConfig && Object.keys(editConfig).length)) {
        return true;
    }
    //获取当前副本
    let copy = this.$store.getters.getPageDomItemObjCopy;
    //如果copy为空，返回真
    if (!(copy && Object.keys(copy).length)) {
        return true;
    }
    //获取当前编辑的item的Obj对象
    let origin = this.getCurrentVDO(copy.id);
    //如果copy对象为空或者副本与原本相同，返回真
    if (this.judgeObjEqual(copy, origin)) {
        return true;
    } else {
        return false;
    }


}

//xxy 新的后台管理方法

//给页面内的某个组件内的config赋值
/**
 * 
 * @param {*} id 
 * @param {*} config 
 * @param {*} part 
 */
Vue.prototype.setEditConfigNew = function (id, config, part) {
    let pageName = getPageName();
    let list = window.manifest[pageName][part];
    //console.log('88888888888888list', part, window.manifest[pageName]);
    let current = list.head;
    while (current) {
        let item = current.element;
        //找到要设置config的对应id的组件
        if (item.id === id) {
            item.config = Object.assign({}, item.config, config);
            initComponentsList();
            break;
        }
        current = current.next;
    }
}
//获取当前配置页面的组件
Vue.prototype.getCurrentVDONew = function (id, part) {
	let index = indexs[part];
    let pageTree = this.$store.getters['gw/getRealComponentsList'][0][index] || [];
    if (id) {
        for(var i=0;i<pageTree.length;i++){
        	if(pageTree[i].id===id){
                return pageTree[i]
        	}
        }
    }
}

//给某个页面的config赋值
Vue.prototype.setPageConfigNew = function (config) {
    let pageName = getPageName();
    //window.manifest[pageName].config = config;
    Object.assign(window.manifest[pageName].config,config)
    initComponentsList();
}
